package com.example.mosbytest.ui.presenter;


import com.example.mosbytest.api.Listener;
import com.example.mosbytest.api.NewsApi;
import com.example.mosbytest.ui.bean.News;
import com.example.mosbytest.ui.view.NewsView;
import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;

import java.util.List;

/**
 * @author lizhangqu
 * @since 2016-02-21 11:25
 */
public class NewsPresenter extends MvpBasePresenter<NewsView> {
    public void loadNews(final boolean pullToRefresh) {
        if (isViewAttached()) {
            getView().showLoading(pullToRefresh);
        }
        Listener<List<News>> listener=new Listener<List<News>>() {
            @Override
            public void onSuccess(List<News> news) {
                if (isViewAttached()) {
                    getView().setData(news);
                    getView().showContent();
                }
            }
            @Override
            public void onFailure(int code) {
                if (isViewAttached()) {
                    getView().showError(new Exception("msg:"+code), pullToRefresh);
                }
            }
        };

        NewsApi.loadNews(pullToRefresh,listener);
    }
}
